# coding: utf-8
# 18-11-15 下午7:55

# ==== log ====

import os
from .local_settings import LOG_LEVEL, DEBUG, LOGGGER_NAMES

LOG_DIR = "/data/log/"

if not os.path.exists(LOG_DIR):
    # 如果没有日志文件夹，先创建
    os.makedirs(LOG_DIR)

LOG_FORMATTER = '[%(asctime)s] %(levelname)s %(pathname)s %(funcName)s %(lineno)d s trace_id: %(trace_id)s : %(message)s'  # noqa
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': LOG_FORMATTER
        },
        'simple': {
            'format': '%(asctime)s %(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': LOG_LEVEL,
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
        'sentry': {
            'level': 'WARNING',
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',  # noqa
            "encoding": "utf8",
        },

    },
    'loggers': {
        'django': {
            'level': LOG_LEVEL,
            'handlers': ['console'],
            'propagate': True,
        },
        'raven': {
            'level': 'ERROR',
            'handlers': ['sentry'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'ERROR',
            'handlers': ['sentry'] + ['console'] if DEBUG else [],
            'propagate': False,
        },
    },
    'root': {
        'level': LOG_LEVEL,
        'handlers': ['console'],
        'propagate': True,
    },
}

for item in LOGGGER_NAMES:

    LOGGING['handlers'].update({
        item: {
            'class': 'logging.handlers.RotatingFileHandler',
            'maxBytes': 1024 * 1024 * 200,
            'backupCount': 5,
            'filename': os.path.join(LOG_DIR, item + '.log'),
            'formatter': 'verbose',
            "encoding": "utf8",
        },
    })

    LOGGING['loggers'].update({
        item: {
            'level': LOG_LEVEL,
            'handlers': [item] + ['console'] if DEBUG else [item],
            'propagate': False,
        },
    })
